#!/bin/bash
set -ex

# Current supported architectures
export ALL_TARGET_ARCH=(amd64 arm32v6 arm64v8)

# Architecture used in tags with no architecture specified (certbot/certbot:latest, certbot/certbot:v0.35.0, ...)
export DEFAULT_ARCH=amd64

# Name of the Certbot Docker organizaation on GitHub. After creating
# repositories with the same names (e.g. "certbot", "dns-dnsmadeeasy", etc.)
# using a different account on Docker Hub, you can change this value to have
# the scripts modify those Docker repositories rather than the repositories for
# the official Certbot Docker images.
export DOCKER_HUB_ORG="certbot"

# List of Certbot plugins
export CERTBOT_PLUGINS=(
    "dns-dnsmadeeasy"
    "dns-dnsimple"
    "dns-ovh"
    "dns-cloudflare"
    "dns-cloudxns"
    "dns-digitalocean"
    "dns-google"
    "dns-luadns"
    "dns-nsone"
    "dns-rfc2136"
    "dns-route53"
    "dns-gehirn"
    "dns-linode"
    "dns-sakuracloud"
)

# Returns the translation from Docker to QEMU architecture
# Usage: GetQemuArch [amd64|arm32v6|arm64v8]
GetQemuArch() {
    ARCH=$1

    case "$ARCH" in
        "amd64")
            echo "x86_64"
            ;;
        "arm32v6")
            echo "arm"
            ;;
        "arm64v8")
            echo "aarch64"
            ;;
        "*")
            echo "Not supported build architecture '$1'." >&2
            exit -1
    esac
}

# Downloads QEMU static binary file for architecture
# Usage: DownloadQemuStatic [x86_64|arm|aarch64]
DownloadQemuStatic() {
    ARCH=$1

    QEMU_ARCH=$(GetQemuArch "$ARCH")
    if [ ! -f "qemu-${QEMU_ARCH}-static" ]; then
        QEMU_DOWNLOAD_URL="https://github.com/multiarch/qemu-user-static/releases/download"
        QEMU_LATEST_TAG=$(curl -s https://api.github.com/repos/multiarch/qemu-user-static/tags \
            | grep 'name.*v[0-9]' \
            | head -n 1 \
            | cut -d '"' -f 4)
        curl -SL "${QEMU_DOWNLOAD_URL}/${QEMU_LATEST_TAG}/x86_64_qemu-$QEMU_ARCH-static.tar.gz" \
            | tar xzv
    fi
}

# Executes the QEMU register script
# Usage: RegisterQemuHandlers
RegisterQemuHandlers() {
    docker run --rm --privileged multiarch/qemu-user-static:register --reset
}

# Builds docker certbot core image for a specific architecture.
# Usage: BuildDockerCoreImage [amd64|arm32v6|arm64v8]
BuildDockerCoreImage() {
    ARCH=$1

    QEMU=$(GetQemuArch "$ARCH")
    docker build \
        --build-arg TARGET_ARCH="${ARCH}" \
        --build-arg QEMU_ARCH="${QEMU}" \
        -f "${DOCKERFILE_PATH}" \
        -t "${DOCKER_REPO}:${ARCH}-${DOCKER_TAG}" \
        .
}

# Builds docker certbot plugin image for a specific architecture and tag.
# Usage: BuildDockerPluginImage [amd64|arm32v6|arm64v8]
BuildDockerPluginImage() {
    ARCH=$1

    QEMU=$(GetQemuArch "$ARCH")
    BASE_IMAGE="${DOCKER_HUB_ORG}/certbot:${TARGET_ARCH}-${DOCKER_TAG}"
    docker build \
        --build-arg BASE_IMAGE="${BASE_IMAGE}" \
        --build-arg QEMU_ARCH="${QEMU}" \
        -f "${DOCKERFILE_PATH}" \
        -t "${DOCKER_REPO}:${ARCH}-${DOCKER_TAG}" \
        .
}

# Pushes docker image for a specific architecture.
# Usage: BuildDockerCoreImage [amd64|arm32v6|arm64v8]
PushDockerImage() {
    ARCH=$1

    docker push "${DOCKER_REPO}:${ARCH}-${DOCKER_TAG}"
}

# Creates any docker images aliases for a given architecture.
# If DOCKER_TAG tag is a version tag such as v0.35.0, the "latest" tag is also
# updated. In the case of the default architecture, tags without the
# architecture part are also created.
# As an example, for amd64 (the default architecture) and the tag v0.35.0, the
# following tags would be created:
#  - certbot/certbot:v0.35.0
#  - certbot/certbot:latest
#  - certbot/certbot:amd64-latest
# For the architecture arm32v6 and the tag v0.35.0, only the following tag
# would be created:
#  - certbot/certbot:arm32v6-latest
# For other tags such as "nightly", aliases are only created for the default
# architecture where the tag "nightly" would be used without an architecture
# part.
# Usage: TagDockerImageAliases [amd64|arm32v6|arm64v8]
TagDockerImageAliases() {
    ARCH=$1

    if [[ "${DOCKER_TAG}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        docker tag "${DOCKER_REPO}:${ARCH}-${DOCKER_TAG}" "${DOCKER_REPO}:${ARCH}-latest"
        if [ "${ARCH}" == "${DEFAULT_ARCH}" ]; then
            docker tag "${DOCKER_REPO}:${ARCH}-${DOCKER_TAG}" "${DOCKER_REPO}:latest"
        fi
    fi
    if [ "${ARCH}" == "${DEFAULT_ARCH}" ]; then
        docker tag "${DOCKER_REPO}:${ARCH}-${DOCKER_TAG}" "${DOCKER_REPO}:${DOCKER_TAG}"
    fi
}

# Pushes docker images aliases created by the TagDockerImageAliases function.
# Usage: PushDockerImageAliases [amd64|arm32v6|arm64v8]
PushDockerImageAliases() {
    ARCH=$1

    if [[ "${DOCKER_TAG}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        docker push "${DOCKER_REPO}:${ARCH}-latest"
        if [ "${ARCH}" == "${DEFAULT_ARCH}" ]; then
            docker push "${DOCKER_REPO}:latest"
        fi
    fi
    if [ "${ARCH}" == "${DEFAULT_ARCH}" ]; then
        docker push "${DOCKER_REPO}:${DOCKER_TAG}"
    fi
}
